home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2002 November / SGI IRIX 6.5 Applications 2002 November.iso / dist / gateway.idb / usr / WebFace / Source / 20-NetworkServices / dhcp / dhcp-sopts.frm.z / dhcp-sopts.frm
Encoding:
Text File  |  2002-06-12  |  11.0 KB  |  352 lines

  1. #!/usr/bin/perl5
  2. #
  3. # dhcp-sopts.cgi
  4. #
  5. # Copyright 1988-1996 Silicon Graphics, Inc.
  6. # All rights reserved.
  7. #
  8. # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  9. # the contents of this file may not be disclosed to third parties, copied or
  10. # duplicated in any form, in whole or in part, without the prior written
  11. # permission of Silicon Graphics, Inc.
  12. #
  13. # RESTRICTED RIGHTS LEGEND:
  14. # Use, duplication or disclosure by the Government is subject to restrictions
  15. # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  16. # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  17. # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  18. # rights reserved under the Copyright Laws of the United States.
  19. #
  20. # $Id: dhcp-sopts.frm,v 1.29 1997/11/17 19:04:51 shotes Exp $
  21.  
  22. use Sys::Hostname;
  23. BEGIN { require "/usr/WebFace/lib/CGI.pm"; import CGI; }
  24. require "/usr/OnRamp/lib/OnRamp.pm";
  25. require "/usr/OnRamp/lib/java.pm";
  26.  
  27. $query = new CGI;
  28.  
  29. $title = "DHCP Server Setup";
  30. $help_page = "dhcp-sopts-help.html";
  31.  
  32. $js =
  33. "which = \"none\";
  34. $js_help
  35. function runSubmit() {
  36.     // onClick gets processed before onSubmit
  37.     if(which == \"ok\") return runOK();
  38.     return (true);
  39. }
  40. function markOK() { which = \"ok\"; }
  41. function markOther() { which = \"none\"; }
  42. function runOK() {
  43.     form = document.DHCPForm;
  44.     if (!testFilename(form.hosts_map)) return (false);
  45.     if (!testFilename(form.ethers_map)) return (false);
  46.     return (true);
  47. }
  48. function testFilename(Ctrl) {
  49.     word = Ctrl.value;
  50.     if (word == \"\") { errorBox (Ctrl, \"The filename is required.\"); return (false); }
  51.     if (!testMetaChars(word)) { errorBox (Ctrl, \"Filename \" + word + \" cannot \\ncontain \" + illegal + \" characters.\"); return (false); }
  52.     for(i = 0; i < word.length; i++) {
  53.         if (word.charAt(i) == ' ') { errorBox (Ctrl, \"Filename \" + word + \" contains spaces.\"); return (false); }
  54.     }
  55.     if (word.charAt(0) != '/') { errorBox (Ctrl, \"Filename \" + word + \" must be fully qualified.\"); return (false); }
  56.     if (!testBadNames(word)) { errorBox (Ctrl, word + \" is a reserved directory.\"); return (false); }
  57.     return (true);
  58. }
  59. function testMetaChars(word) {
  60.     metaChars = \"{}()*!~<>?|[]'&^\$\@\#`\\\";:\";
  61.     for(j = 0; j < metaChars.length; j++) {
  62.         illegal = metaChars.charAt(j);
  63.         if (word.indexOf (illegal, 0) != -1) return (false);
  64.     } return (true);
  65. }
  66. function testBadNames(word) {
  67.     if (word == \"/\" || word == \"/usr\" || word == \"/etc\" || word == \"/bin\" || word == \"/sbin\" || word == \"/usr\" || word == \"/usr/sbin\") return (false);
  68.     return (true);
  69. }
  70. function errorBox (Ctrl, ErrorMessage) {
  71.     alert (ErrorMessage);  Ctrl.focus();  return;
  72. }";
  73.  
  74.  
  75. print $query->header;
  76.  
  77. $help = $document_root . $ENV{"SCRIPT_NAME"};
  78. $help =~ s/cgi$/hlp/;
  79. exec $help if ($query->param('help') eq "Help");
  80.  
  81. &js_title_block($title,$js);
  82.  
  83. $conf = "/etc/inetd.conf";
  84. $osf = $def_osf = "/etc/config/dhcp_bootp.options";
  85.  
  86. $dummy = "/etc/inetd.conf.tmp";
  87.  
  88. $def_hosts = "/etc/hosts";
  89. $def_ethers = "/etc/ethers";
  90. $def_sys = "/unix";
  91. $def_propel_dbm = "/var/IRIXpro";
  92. $def_config_dir = "/var/dhcp/config";
  93. $host = hostname;
  94. $def_propel_mgr = "$host:1525";
  95. $dbfile = "/usr/IRIXpro/lib/system.dbtclrc";
  96. $use_propel = "No";
  97.  
  98. $rly_off = system ("/etc/chkconfig", "proclaim_relayagent");
  99. $srv_off = system ("/etc/chkconfig", "proclaim_server");
  100. $def_sstate = "No";
  101. if ($srv_off == 0) {
  102.     $def_sstate = "Yes";
  103. }
  104.  
  105. if ($def_sstate eq "Yes") { &getFilename; }
  106.  
  107. if ($query->param) {
  108.     &formValidation;
  109.     if ($query->param('doit') eq 'Ok') {    
  110.         $new_sstate = $query->param('sstate');
  111.         &error("You must first disable DHCP relay agent before you can
  112.             enable DHCP server.") if ($new_sstate eq "Yes" && !$rly_off);
  113.         if ($new_sstate ne $def_sstate) {            # state changed
  114.             if ($new_sstate eq "Yes") {
  115.                 system ("/etc/chkconfig", "autoconfig_ipaddress", "off");
  116.                 system ("/etc/chkconfig", "proclaim_server", "on");
  117.             } else {
  118.                 system ("/etc/chkconfig", "proclaim_server", "off"); 
  119.             }
  120.         }
  121.         if ($new_sstate eq 'Yes') { &makeChanges; }
  122.     }
  123.     print "<i>The new state has been saved.</i>\n";
  124. }
  125.  
  126. if ($def_sstate eq "Yes") { &readConfig; }
  127.  
  128. &generic;
  129.  
  130. sub formValidation {
  131.     my $error = &check_fname($query->param('hosts_map'), 1);
  132.     &error($error) if $error;    
  133.     
  134.     $error = &check_fname($query->param('ethers_map'), 1);
  135.     &error($error) if $error;
  136. }
  137.  
  138. sub error {
  139.     &error_block($_[0]);
  140.     &generic;
  141.     exit 0;
  142. }
  143.  
  144. sub makeChanges {
  145. #    if ($query->param('opt_file') ne $osf) {
  146. #        $osf = $query->param('opt_file');
  147. #        $value = "bootp\tdgram\tudp\twait\troot\t/usr/etc/bootp\tbootp\t-o $osf\n";
  148. #        &putEntry($conf,"bootp",$value); 
  149. #    }
  150.  
  151.     $rename = 0;
  152.     open(IN,"< $conf");
  153.     open(OUT,"> $dummy");
  154.     while(<IN>) {
  155.         @items = split(/\s+/);
  156.         if ($items[6] eq "bootp" || $items[7] eq "bootp") {
  157.         if ($items[0] eq "bootp") {
  158.             print OUT "# $_";
  159.             $rename = 1;
  160.         }
  161.         }
  162.         elsif($items[6] eq "dhcp_bootp" || $items[7] eq "dhcp_bootp") {
  163.         if ($items[0] ne "bootp") {
  164.             $line = "bootp\tdgram\tudp\twait\troot\t/usr/etc/dhcp_bootp ";
  165.             $line .= "dhcp_bootp -o /etc/config/dhcp_bootp.options\n";
  166.             print OUT $line;
  167.             $rename = 1;
  168.         }
  169.         }
  170.         else { print OUT $_; }
  171.     }
  172.     close(IN);
  173.     close(OUT);
  174.     if ($rename) { rename($dummy,$conf); }        
  175.  
  176.     open(OUT,"> $osf");
  177.  
  178.     if ($query->param('use_propel') eq 'Yes') {
  179.         print OUT "-s ", $query->param('propel_dbms'), "\n";
  180.         if ($query->param('propel_mgr') ne $def_propel_mgr) {
  181.             print OUT "ih ", $query->param('propel_mgr'), "\n";
  182.         }
  183.     }
  184.     if ($query->param('hosts_map') ne '/etc/hosts') {
  185.         print OUT "-w ", $query->param('hosts_map'), "\n"; }
  186.     if ($query->param('ethers_map') ne '/etc/ethers') {
  187.         print OUT "-e ", $query->param('ethers_map'), "\n"; }
  188. #    if ($query->param('sys_name') ne '/unix') {
  189. #        print OUT "-u ", $query->param('sys_name'), "\n"; }
  190. #    if ($query->param('config_dir') ne '/var/dhcp/config') {
  191. #        print OUT "-c ", $query->param('config_dir'), "\n"; }
  192.     close(OUT);
  193. }
  194.  
  195. sub putEntry { 
  196.     local($file) = $_[0];
  197.     local($key) = $_[1];
  198.     local($val) = $_[2];
  199.     local($dummy) = "dummy";
  200.  
  201.     open(IN,"< $file");
  202.     open(OUT,"> $dummy");
  203.     local($found) = 0;
  204.     while(<IN>) {
  205.         if (/^$key/ || /^#\s*$key/) {
  206.             print OUT $val,"\n";
  207.             $found = 1;
  208.         } else { print OUT $_; }
  209.     }
  210.     if ($found == 0) { print OUT $val,"\n"; }
  211.     rename($dummy, $file) || &error("Unable to rename $dummy to $file");
  212.     close(IN);
  213.     close(OUT);
  214. }
  215.  
  216. sub getFilename {
  217.     open(INETD, $conf) || &error("Unable to open $conf");
  218.     $osf = "";
  219.     while (<INETD>) {
  220.     if (/^#/) {
  221.         next;
  222.     }
  223.     # bootp dgram udp wait root /usr/etc/dhcp_bootp dhcp_bootp -o <file>
  224.     if (/^bootp/) {
  225.         chop;
  226.         @entlist = split(/\s+/);
  227.         if (($entlist[6] eq "dhcp_bootp") && ($entlist[7] eq "-o")) {
  228.         $osf = $entlist[8];
  229.         last;    # Break out of the while loop
  230.         }
  231.     }
  232.     }
  233.     close(INETD);
  234.     if (!$osf) { $osf = $def_osf; }
  235. }
  236.  
  237. sub readConfig {
  238.     open(OPTIONS, $osf) || &error("Unable to open $osf");
  239.     while(<OPTIONS>) {
  240.     if (/^#/) {
  241.         next;
  242.     }
  243.     chop;
  244.     @optlist = split(/\s+/);
  245.     $i = 0;
  246.     while ($optlist[$i]) {
  247.         if ($optlist[$i] eq "-y") {
  248.         &error("Option $optlist[$i] in file $osf is no longer supported");
  249.         }
  250.         elsif ($optlist[$i] eq "-s") {
  251.         $def_propel_dbm = $optlist[$i+1];
  252.         $use_propel = "Yes";
  253.         $i += 2;
  254.         next;
  255.         }
  256.         elsif ($optlist[$i] eq "-h") {
  257.         $def_propel_mgr  = $optlist[$i+1];
  258.         $i += 2;
  259.         next;
  260.         }
  261.         elsif ($optlist[$i] eq "-w") {
  262.         $def_hosts = $optlist[$i+1];
  263.         $i += 2;
  264.         next;
  265.         }
  266.         elsif ($optlist[$i] eq "-e") {
  267.         $def_ethers = $optlist[$i+1];
  268.         $i += 2;
  269.         next;
  270.         }
  271.         elsif ($optlist[$i] eq "-u") {
  272.         $def_sys = $optlist[$i+1];
  273.         $i += 2;
  274.         next;
  275.         }
  276.         elsif ($optlist[$i] eq "-c") {
  277.         $def_config_dir = $optlist[$i+1];
  278.         $i += 2;
  279.         next;
  280.         }
  281.         else {
  282.         &error("Option $optlist[ei] in file $osf is not recognized");
  283.         }
  284.         $i += 1;
  285.     }
  286.     }
  287.     close(OPTIONS);
  288. }
  289.  
  290. sub generic {
  291.     &header_block($title);
  292.  
  293.     print $query->startform("POST", "", "", "NAME=DHCPForm", "onSubmit=\"return runSubmit()\"");
  294.  
  295.     print "<center ><table cellpadding=5 width=450>\n";
  296.  
  297.     print "<tr><th align=left>Enable DHCP server:</th><th align=left>\n",
  298.               $query->radio_group(-name=>'sstate',
  299.               -values=>['Yes','No'],
  300.               -default=>$def_sstate), "</th></tr>";
  301.  
  302. #    print "<tr><th align=left>DHCP Server Option File:</th><th align=left>\n",
  303. #              $query->textfield(-name=>'opt_file',
  304. #            -default=>$osf,
  305. #            -size=>20, -maxlength=>256), "</th></tr>";
  306.  
  307.     if (-e $dbfile) {
  308.             print "<hr><tr><th align=left>Use the Propel database for the hosts 
  309.                 and ethers maps:</th><th align=left>\n",
  310.               $query->radio_group(-name=>'use_propel',
  311.                   -values=>['Yes','No'],
  312.                   -default=>$use_propel), "</th></tr>";
  313.             print "<tr><th align=left>Directory for Propel database:</th><th align=left>\n",
  314.               $query->textfield(-name=>'propel_dbms',
  315.                 -default=>$def_propel_dbm,
  316.                 -size=>20, -maxlength=>256), "</th></tr>";
  317.             print "<tr><th align=left>Hostname of Propel lock manager:</th><th align=left>\n",
  318.               $query->textfield(-name=>'propel_mgr',
  319.                 -default=>$def_propel_mgr,
  320.                 -size=>20, -maxlength=>256), "</th></tr>";
  321.     }
  322.  
  323.     print "<tr><th align=left>File name for translating IP addresses to 
  324.             hostnames:</th><th align=left>\n",
  325.               $query->textfield(-name=>'hosts_map',
  326.             -default=>$def_hosts,
  327.             -size=>20, -maxlength=>256), "</th></tr>";
  328.     print "<tr><th align=left>File name for translating ethernet address to 
  329.             hostnames:</th><th align=left>\n",
  330.               $query->textfield(-name=>'ethers_map',
  331.             -default=>$def_ethers,
  332.             -size=>20, -maxlength=>256), "</th></tr>";
  333.  
  334. #    print "<tr><th align=left>System File Name:</th><th align=left>\n",
  335. #              $query->textfield(-name=>'sys_name',
  336. #            -default=>$def_sys,
  337. #            -size=>20, -maxlength=>256), "</th></tr>";
  338. #    print "<tr><th align=left>DHCP Config Directory:</th><th align=left>\n",
  339. #              $query->textfield(-name=>'config_dir',
  340. #            -default=>$def_config_dir,
  341. #            -size=>20, -maxlength=>256), "</th></tr>";
  342.  
  343.     print "</table></center><br>";
  344.  
  345.     print &js_buttons('doit','Ok','onClick="markOK()"','onClick="markOther()"',
  346.         "onClick=\"do_help('$help_page'); return (false)\"");
  347.  
  348.     print $query->endform;
  349.  
  350.     print $query->end_html;
  351. }
  352.